package org.shs.company.service;

import static org.shs.common.config.SystemInitializer.app_msgs;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.shs.common.dao.CommonDAOFactory;
import org.shs.common.utils.DataHolder;
import org.shs.company.dao.ICompanyDAO;
import org.shs.company.form.CompanyForm;
import org.shs.company.model.CompanyModel;

/**
 * The service class to encapsulate all the business logic for handling the
 * data related to company screens
 * <br/>
 * <br/><b>Created:</b>&nbsp;&nbsp; 16-Jan-2009 00:51:42
 *
 * @author anirvan
 *
 */
public class CompanyService
{
   /**
    * The reference for the DAO interface associated with this 
    * service class.
    */
   private ICompanyDAO companyDao = null;
   
   /**
    * The logger instance to be used
    */
   private static Logger logger = Logger.getLogger(CompanyService.class);
   
   /**
    * The default constructor
    */
   public CompanyService()
   {
      try
      {
         //Initialize the DAO instance
         companyDao = (ICompanyDAO)CommonDAOFactory
                        .getDAO(app_msgs.getMessage("company.daoImpl"));
      }
      catch (Exception e) 
      {
         logger.error("[Possible cause] No key present for {company.daoImpl}.", e);
      }
   }
   
   /**
    * Retrieves the list of companies. Used for general retrieval as well as
    * a search scenario
    * @param companyName the description of the company to be searched
    * @param srchInactive active status search flag
    * @return the list of companies
    */
   public ArrayList<CompanyModel> getCompanies(String companyName,
                                               String srchInactive)
   {
      ArrayList<CompanyModel> companies = null;
      DataHolder data = new DataHolder();
      
      try
      {
         // This is going to be a wild-card contains query
         companyName = (companyName == null) ? "%%"
                         : "%" + companyName + "%";
         data.setName(companyName);
         data.setStrVal(srchInactive);
         // Fetch the list
         companies = companyDao.getCompanies(data);
      }
      catch (Exception e)
      {
         logger.error(String.format("Could not retrieve the list of companies " 
                                    + "for [%s]", companyName), e);
      }
      
      return companies;
   }
   
   /**
    * De-activates/Activates a company
    * @param companyData The data about the company
    * @return the status of all de-activation operations
    */
   public boolean updActiveStatus(String[] companyIds, String actvStatus)
   {
      boolean status = false;
      DataHolder data = null;
      
      try
      {
         data = new DataHolder();
         data.setStrVal(actvStatus);
         // Update status of all companies
         for (String companyId : companyIds)
         {
            data.setName(companyId);
            companyDao.updActiveStatus(data);
         }
         status = true;
      }
      catch (Exception e)
      {
         logger.error("Could not update active status for the company", e);
      }
      
      return status;
   }
   
   /**
    * Updates the details for an existing company
    * @param companyData The data about the company
    * @return the updated company details
    */
   public CompanyModel editCompanyDetails(CompanyForm companyData)
   {
      CompanyModel savedCompanyData = null;
      
      try
      {
         // Edit the company data
         companyDao.editCompanyData(companyData);
         // Retrieve the company details to display on the screen
         savedCompanyData = companyDao.getCompanyDetails(
                                                   companyData.getCompanyId());
      }
      catch (Exception e)
      {
         logger.error(String.format("Could not update the company data - " +
                                    "[{%s} - %s].", 
                                    companyData.getCompanyId(),
                                    companyData.getCompanyDesc()), 
                      e);
      }
      
      return savedCompanyData;
   }
   
   /**
    * Adds the details for a new company
    * @param companyData The data about the new company
    * @return the details about the added company
    */
   public CompanyModel addCompanyDetails(CompanyForm companyData)
   {
      String companyId = null;
      CompanyModel savedCompanyData = null;
      
      try
      {
         // Add the company data
         companyId = companyDao.addNewCompanyData(companyData);
         if (companyId != null)
         {
            companyData.setCompanyId(companyId);
         }
         
         // Retrieve the company details to display on the screen
         savedCompanyData = companyDao.getCompanyDetails(companyId);
      }
      catch (Exception e)
      {
         logger.error("Could not save the company data.", e);
      }
      
      return savedCompanyData;
   }
   
   /**
    * Retrieves the details for a particular company
    * @param companyId The ID of the company
    * @return The model encapsulating all details about the company
    * @throws Exception if query resulted in an error.
    */
   public CompanyModel getCompanyDetails(String companyId)
   {
      CompanyModel company = null;
      
      try
      {
         // Fetch the details
         company = companyDao.getCompanyDetails(companyId);
      }
      catch (Exception e)
      {
         logger.error(String.format("Could not retrieve the company details " +
                                    " for ID [%s]", companyId), e);
      }
      
      return company;
   }
}
